package com.latam.arq.example.web.admin.managedbeans;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.latam.arq.commons.appconfig.properties.AppConfigException;
import com.latam.arq.commons.appconfig.properties.AppConfigManagerException;
import com.latam.arq.commons.appconfig.properties.AppConfigManagerService;
import com.latam.arq.commons.appconfig.properties.AppConfigUtil;
import com.latam.arq.commons.web.utils.MessageUtil;

/**
 * ExamplePropertiesViewerMB
 * Example ManagedBean to accesss the actual properties files.
 * (According to the project requirements, this class must be removed or moved to the project package.) 
 * 
 * @author LATAM Enterprise Architect
 * @author francisco.mendez@lan.com (Francisco Mendez)
 *
 */
@ManagedBean(name = "examplePropertiesViewer")
@ViewScoped
public class ExamplePropertiesViewerMB {
	private static final Logger logger = LoggerFactory.getLogger(ExamplePropertiesViewerMB.class);
	
	//Example list of properties to hide 
	private static final String[] PROPERTIES_TO_HIDE = new String[]{ 
		"test.password" 
	}; 
	
	@EJB
	private AppConfigManagerService appConfigManager;	
	
	private String actualPropertiesFileName;
	private final List<Map.Entry<Object, Object>> actualPropertiesList = new ArrayList<Map.Entry<Object, Object>>();
	private List<Map.Entry<Object, Object>> filteredActualPropertiesList;
	
	private final List<File> propertyFiles = new ArrayList<File>();
	
	@PostConstruct
	void init(){
		try {
			final File appConfigFolder = AppConfigUtil.getFileFromAppFolder(".");
			for (final File file : appConfigFolder.listFiles()) {
				if (file.getName().endsWith(".properties")){
					propertyFiles.add(file);
				}
			}
		} catch (final AppConfigException e) {
			logger.error("Error reading properties files folder", e);
		}
		
	}
	
	/**
	 * Returns The properties files list
	 * 
	 * @return
	 */
	public List<File> getPropertiesFileList() {
		return propertyFiles;
	}

	private Properties getPropertiesFile(String fileName) throws IOException, AppConfigException {
		final Properties properties = new Properties();
		properties.load(AppConfigUtil.getResourceFromAppFolder(fileName));
		return properties;
	}
	
	public String getActualPropertiesFileName() {
		return actualPropertiesFileName;
	}

	public void setActualPropertiesFileName(String fileName) {
		this.actualPropertiesFileName = fileName;
		
		this.actualPropertiesList.clear();
		this.filteredActualPropertiesList = null;

		try {
			final Properties properties = getPropertiesFile(fileName);
			if (properties!=null){
				actualPropertiesList.addAll(properties.entrySet());
				
				for(final Map.Entry<Object, Object> entry:properties.entrySet()){
					boolean hide = false;
					for(String toHide:PROPERTIES_TO_HIDE){
						if (entry.getKey().toString().toLowerCase().equals(toHide)){
							hide = true;
							break;
						}
					}
					if (hide){
						actualPropertiesList.remove(entry);
					}
				}
				
			}
		} catch (IOException e) {
			logger.error("IOException: Error reading properties file " + fileName, e);
		} catch (AppConfigException e) {
			logger.error("AppConfigException: Error reading properties file " + fileName, e);
		}

	}

	public List<Map.Entry<Object, Object>> getActualPropertiesList() {
		return actualPropertiesList;
	}

	public List<Map.Entry<Object, Object>> getFilteredActualPropertiesList() {
		return filteredActualPropertiesList;
	}

	public void setFilteredActualPropertiesList(List<Map.Entry<Object, Object>> filteredActualPropertiesList) {
		this.filteredActualPropertiesList = filteredActualPropertiesList;
	}
	
	public void reloadProperties(){
		try{
			appConfigManager.reloadProperties();
		}catch(AppConfigManagerException e){
			MessageUtil.addErrorMessage("admin.properties.reload.error",null);
		}
	}

}
